package com.hyperboost.controller;

import com.github.pagehelper.PageHelper;
import com.hyperboost.constant.MessageConstant;
import com.hyperboost.dto.FrontUserDTO;
import com.hyperboost.dto.SharePageQueryDto;
import com.hyperboost.entity.FrontUser;
import com.hyperboost.properties.JwtProperties;
import com.hyperboost.result.PageResult;
import com.hyperboost.result.Result;
import com.hyperboost.service.ShareCommentService;
import com.hyperboost.service.ShareService;
import com.hyperboost.service.UserService;
import com.hyperboost.utils.JwtUtil;
import com.hyperboost.utils.ThreadLocalUtil;
import com.hyperboost.vo.FrontUserVO;
import com.hyperboost.vo.ShareCommentReplyVO;
import com.hyperboost.vo.ShareVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户相关接口
 */
@Slf4j
@RestController
@RequestMapping("/frontend/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ShareService shareService;

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private ShareCommentService shareCommentService;

    /**
     * 用户注册
     *
     * @param frontUserDto
     * @return 注册结果
     */
    @PostMapping("/register")
    private Result register(@RequestBody FrontUserDTO frontUserDto) {

        log.info("用户注册: {}", frontUserDto);

        //1.用户注册，调用userService.register()方法
        userService.register(frontUserDto);
        //2.返回注册结果
        return Result.success(MessageConstant.REGISTER_SUCCESS);
    }

    /**
     * 用户登录
     *
     * @param frontUserDto
     * @return
     */
    @PostMapping("/login")
    public Result<FrontUserVO> login(@RequestBody FrontUserDTO frontUserDto) {

        log.info("用户登录: {}", frontUserDto);

        // 1.用户登录，调用userService.login()方法
        FrontUserVO frontUserVO = userService.login(frontUserDto);
        // 2.登录成功，生成JWT-Token
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", frontUserVO.getUserId());
        claims.put("username", frontUserVO.getUsername());
        System.out.println("claims: " + claims.get("userId").getClass());
        String token = JwtUtil.createToken(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        // 3.设置token到frontUserVO对象中
        frontUserVO.setToken(token);

        // 3.返回登录结果
        return Result.success(MessageConstant.LOGIN_SUCCESS, frontUserVO);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public Result<FrontUserVO> info() {

        // 1.通过ThreadLocalUtil获取当前登录用户信息
        Map<String, Object> map = ThreadLocalUtil.get();

        // 2.获取当前登录用户信息，调用userService.getUserByUsername()方法
        Long userId = ((Number) map.get("userId")).longValue();
        FrontUserVO frontUserVo = userService.getUserByUserId(userId);

        log.info("获取用户信息，当前登录用户ID: {}", userId);

        // 3.返回用户信息
        return Result.success(MessageConstant.INFO_SUCCESS, frontUserVo);

    }

    /**
     * 更新用户信息
     *
     * @param frontUserDto
     * @return
     */
    @PostMapping("/update")
    public Result<FrontUserVO> update(@RequestBody FrontUserDTO frontUserDto) {

        log.info("更新用户信息: {}", frontUserDto);

        // 1.更新用户信息，调用userService.update()方法
        userService.update(frontUserDto);

        // 2.获取用户信息
        FrontUserVO frontUserVo = userService.getUserByUserId(frontUserDto.getUserId());

        // 2.返回更新结果
        return Result.success(MessageConstant.UPDATE_SUCCESS, frontUserVo);
    }

    /**
     * 更新用户密码
     *
     * @param passwordMap
     * @return
     */
    @PostMapping("/update/password")
    public Result password(@RequestBody Map<String, String> passwordMap) {

        log.info("修改用户密码: {}", passwordMap);

        // 1.更新用户密码，调用userService.updatePassword()方法
        userService.password(passwordMap);

        // 2.返回密码修改结果
        return Result.success(MessageConstant.PASSWORD_SUCCESS);
    }
    /**
     * 查询回复我的评论
     *
     * @param sharePageQueryDto
     * @return
     */
    @GetMapping("/commentReply")
    public Result<PageResult<ShareCommentReplyVO>> CommentReply(SharePageQueryDto sharePageQueryDto) {
        PageResult<ShareCommentReplyVO> ReplyMyComment = shareCommentService.CommentReply(sharePageQueryDto);
        return Result.success(ReplyMyComment);
    }

    /**
     * 查询"我的评论"
     *
     * @param
     * @return
     */
    @GetMapping("/myComment")
    public Result<PageResult<ShareCommentReplyVO>> myComment(SharePageQueryDto sharePageQueryDto) {
        PageResult<ShareCommentReplyVO> myReply = shareCommentService.getMyComment(sharePageQueryDto);
        return Result.success(myReply);
    }


}
